home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / inn1.000 / inn1.4sec-linux-src.tar / inn / config / subst.sh < prev   
Linux/UNIX/POSIX Shell Script  |  1991-11-27  |  2KB  |  102 lines

  1. #! /bin/sh
  2. # subst - substitute strings into files, carefully
  3.  
  4. PATH=/bin:/usr/bin ; export PATH
  5. sed=sed
  6.  
  7. case "$1"
  8. in
  9.     -e)
  10.     sed=$2
  11.     shift ; shift
  12.     ;;
  13. esac
  14.  
  15. case "$1"
  16. in
  17.     -f)
  18.     substs=$2
  19.     shift ; shift
  20.     ;;
  21.     *)
  22.     echo "$0: no substitutions file given" >&2
  23.     exit 2
  24.     ;;
  25. esac
  26.  
  27. them="`${sed}    -e '/^#/d' \
  28.         -e '/^[     ]*$/d' \
  29.         -e '/&/s/&/\\\\&/g' \
  30.         -e 's/^\\([^    ]*\\)        *\\([^    ]*\\)$/s#@<\\1>@#\\2#g/' \
  31.     $substs`"
  32.  
  33.  
  34. for f
  35. do
  36.     # first, figure out temporary names
  37.     case "$f"
  38.     in
  39.         */*)
  40.         file="`expr \"$f\" : '.*/\\([^/]*\\)'`"
  41.         dir="`expr \"$f\" : '\\(.*\\)/[^/]*'`"
  42.         new="$dir/substtmp.new"
  43.         old="$dir/substtmp.old"
  44.         ;;
  45.  
  46.         *)
  47.         new="substtmp.new"
  48.         old="substtmp.old"
  49.         ;;
  50.     esac
  51.     echo "$f: " | tr -d '\012'
  52.  
  53.     # test existences
  54.     if test ! -f $f
  55.     then
  56.         echo "$0: cannot find \`$f'" >&2
  57.         continue                # NOTE CONTINUE
  58.     fi
  59.     if test -r $new
  60.     then
  61.         echo "$0: $new exists, cannot proceed" >&2
  62.         exit 1
  63.     fi
  64.     if test -r $old
  65.     then
  66.         echo "$0: $old exists, cannot proceed" >&2
  67.         exit 1
  68.     fi
  69.     ( >$old >$new ) 2>/dev/null
  70.     if test ! -w "$old" -o ! -w "$new"
  71.     then
  72.         rm -f $old $new
  73.         echo "$0: cannot create temporaries $old $new" >&2
  74.         exit 1
  75.     fi
  76.  
  77.     # generate the new version
  78.     trap "rm -f $new; exit" 1 2 15
  79.     ${sed} "/=()<.*>()=/{
  80.         h
  81.         n
  82.         g
  83.         s/.*=()<//
  84.         s/>()=.*//
  85.         $them
  86.     }" $f >$new
  87.  
  88.     # substitute new for old, if necessary
  89.     if cmp -s $new $f
  90.     then
  91.         rm -f $new $old
  92.         echo "unchanged"
  93.     else
  94.         trap "mv $old $f; exit" 1 2 15
  95.         mv $f $old
  96.         mv $new $f
  97.         trap "rm -f $old; exit" 1 2 15
  98.         rm -f $old
  99.         echo "updated"
  100.     fi
  101. done
  102.